package com.smxy.hunzi.article.service.impl;



import com.smxy.hunzi.article.entity.LabelEntity;
import com.smxy.hunzi.article.openfeign.UserOpenfeignService;
import com.smxy.hunzi.article.service.LabelService;
import com.smxy.hunzi.article.service.ModuleService;
import com.smxy.hunzi.article.vo.ArticleVo;
import com.smxy.hunzi.common.utils.R;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smxy.hunzi.common.utils.PageUtils;
import com.smxy.hunzi.common.utils.Query;

import com.smxy.hunzi.article.dao.ArticleDao;
import com.smxy.hunzi.article.entity.ArticleEntity;
import com.smxy.hunzi.article.service.ArticleService;


@Service("articleService")
public class ArticleServiceImpl extends ServiceImpl<ArticleDao, ArticleEntity> implements ArticleService {
    @Autowired
    private LabelService labelService;
    @Autowired
    private ModuleService moduleService;
    @Autowired
    private UserOpenfeignService userOpenfeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<ArticleEntity> wrapper = new QueryWrapper<>();
        wrapper.select("id","title","module_id","comments_count","likenum_int"
        ,"label","create_time","user_id");
        IPage<ArticleEntity> page = this.page(
                new Query<ArticleEntity>().getPage(params),
                wrapper
        );



        List<ArticleVo> collect = page.getRecords().stream()
                .map(m -> {
                    //System.out.println(m);
                    ArticleVo articleVo = new ArticleVo();
                    BeanUtils.copyProperties(m, articleVo);
                    //时间格式化
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    String format = sdf.format(Date.from(Instant.ofEpochMilli(m.getCreateTime())));

                    String[] split = m.getLabel().split(",");
                    String labels = ""; //标签名字
                    String ModuleName = moduleService.getById(m.getModuleId()).getModuleName(); //版块名字
                    if (split.length > 0) {
                        Collection<LabelEntity> labelEntities =  labelService.listByIds(Arrays.asList(split));
                        labels  =  labelEntities.stream()
                                .map(LabelEntity::getLabelName)
                                .collect(Collectors.joining(","));
                    }
                    if(m.getUserId() != 0L){
                        R info = userOpenfeignService.info(m.getUserId());
                        articleVo.setNickname((String) info.get("nickname"));
                    }
                    else{
                        articleVo.setNickname("admin");
                    }
                    articleVo.setLabelName(labels.split(","));
                    articleVo.setFormatCreateDate(format);
                    articleVo.setModuleName(ModuleName);
                    return articleVo;
                })
                .collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(collect);
        return pageUtils;
    }

}